package com.viking.framework.action;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.googlecode.jsonplugin.JSONException;
import com.googlecode.jsonplugin.JSONUtil;
import com.opensymphony.xwork2.ActionSupport;
import com.viking.utils.EncodingUtils;

public class JasonBaseAction extends ActionSupport implements
		ServletRequestAware, ServletResponseAware {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3872420901217388337L;

	protected HttpServletRequest request;

	protected HttpServletResponse response;

	protected String realPath;

	public void setServletResponse(HttpServletResponse httpServletResponse) {
		this.response = httpServletResponse;
	}

	public void setServletRequest(HttpServletRequest httpServletRequest) {
		this.request = httpServletRequest;
		this.realPath = request.getSession().getServletContext()
				.getRealPath("/");
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	/**
	 * <p>
	 * Description: 执行JSON数据打印操作
	 * </p>
	 * 
	 * @param json
	 */
	protected void printString(String contentType, String str) {
		if (contentType == null || contentType.equals(""))
			contentType = "application/x-json;charset=UTF-8";
		response.setContentType(contentType);
		response.setCharacterEncoding("UTF-8");
		try {
			response.getWriter().write(str);
			response.getWriter().flush();
			response.getWriter().close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	protected void printString(String json) {
		printString("", json.replaceAll("\n", "").replaceAll("\r", ""));
	}

	/**
	 * @param jsonObj
	 * @author Yzc
	 */
	protected void printObjectJson(Object jsonObj) {
		try {
			String str = JSONUtil.serialize(jsonObj);
			// System.out.println("JSON:" + str);
			this.printString("text/html;charset=UTF-8", str);
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	protected void printJson(String json) {
		response.setContentType("application/x-json;charset=UTF-8");
		try {
			// System.out.println("JSON:" + json);
			response.getWriter().write(json);
			response.getWriter().close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	protected String getRealPath() {
		return this.realPath;
	}

	protected void fileExport(InputStream ins, String fileName) {
		response.setContentType("application/x-msdownload;charset=UTF-8");
		response.setHeader("Content-Disposition", "attachment; filename="
				+ EncodingUtils.toUtf8String(fileName));
		try {
			copy(ins, response.getOutputStream());
			response.getOutputStream().close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @param input
	 * @param output
	 * @return
	 * @throws IOException
	 * @see 完成输入流到输出流的字节拷贝
	 */
	private int copy(InputStream input, OutputStream output) throws IOException {
		byte[] buffer = new byte[8192];
		int count = 0;
		int n = 0;
		while (-1 != (n = input.read(buffer))) {
			output.write(buffer, 0, n);
			count += n;
		}
		return count;
	}

	/**
	 * @param xml
	 * 
	 */
	protected void printXml(String xml) {
		this.printString("text/xml", xml);
	}

	/**
	 * @param html
	 * 
	 */
	protected void printHtml(String html) {
		this.printString("text/html", html);
	}

}
